package com.mxpio.erp.plan.service.impl;

import com.mxpio.erp.common.plan.entity.SalePredictionLine;
import com.mxpio.erp.common.plan.entity.SalePredictionOffsettingFlow;
import com.mxpio.erp.common.plan.service.SalePredictionOffsettingFlowService;
import com.mxpio.erp.common.sales.entity.SalesOrder;
import com.mxpio.erp.common.sales.entity.SalesOrderLine;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SalePredictionOffsettingFlowServiceImpl extends BaseServiceImpl<SalePredictionOffsettingFlow> implements SalePredictionOffsettingFlowService {

    @Override
    @Transactional
    public void handleQuantityFromSO(List<SalesOrderLine> salesOrderLineList){
        if (salesOrderLineList!=null && salesOrderLineList.size()>0){
            for (SalesOrderLine line:salesOrderLineList){
                String sumSql = "SELECT ifnull(SUM(QUANTITY_),0) AS QUANTITY_ FROM MB_ERP_PLAN_SP_OF WHERE  SO_BIZ_NO_ = '" + line.getBizNo() + "' AND SO_BIZ_LINE_NO_ = '" + line.getLineNo() + "'";
                BigDecimal sum = (BigDecimal) JpaUtil.nativeQuery(sumSql).getSingleResult();
                line.setActualQuantity (sum==null?BigDecimal.ZERO:sum);
            }
        }
    }
    @Override
    @Transactional
    public void handleQuantityFromSP(List<SalePredictionLine> salePredictionLines){
        if (salePredictionLines!=null && salePredictionLines.size()>0){
            for (SalePredictionLine line:salePredictionLines){
                String sumSql = "SELECT ifnull(SUM(QUANTITY_),0) AS QUANTITY_ FROM MB_ERP_PLAN_SP_OF WHERE  SP_BIZ_NO_ = '" + line.getBizNo() + "' AND SP_BIZ_LINE_NO_ = '" + line.getLineNo() + "'";
                BigDecimal sum = (BigDecimal) JpaUtil.nativeQuery(sumSql).getSingleResult();
                line.setActualQuantity (sum==null?BigDecimal.ZERO:sum);
            }
        }
    }

    @Override
    @Transactional
    public void handleSaveData(List<SalePredictionOffsettingFlow> salePredictionOffsettingFlows) {//关联销售订单客户
        if (salePredictionOffsettingFlows!=null&&salePredictionOffsettingFlows.size()>0){
            List<String> soBizNoList = salePredictionOffsettingFlows.stream().map(SalePredictionOffsettingFlow::getSoBizNo).collect(Collectors.toList());
            List<SalesOrder> salesOrders = JpaUtil.linq(SalesOrder.class).in("bizNo", soBizNoList).list();
            Map<String, SalesOrder> salesOrderMap = JpaUtil.index(salesOrders, "bizNo");
            for (SalePredictionOffsettingFlow flow:salePredictionOffsettingFlows){
                if (StringUtils.isBlank(flow.getPnCode())){
                    if (salesOrderMap.containsKey(flow.getSoBizNo())&&salesOrderMap.get(flow.getSoBizNo())!=null){
                        flow.setPnCode(salesOrderMap.get(flow.getSoBizNo()).getPnCode());
                        flow.setPnName(salesOrderMap.get(flow.getSoBizNo()).getPnName());
                    }
                }
            }
        }
    }
    @Override
    @Transactional(readOnly = false)
    public Page<SalePredictionOffsettingFlow> listPage(Pageable page, Criteria c){
        Page<SalePredictionOffsettingFlow> paging = JpaUtil.linq(SalePredictionOffsettingFlow.class)
                .where(c)
                .paging(page);
        if (paging.getContent().size()>0){
            List<SalePredictionOffsettingFlow> content = paging.getContent();
            List<String> collect = content.stream().map(salePredictionOffsettingFlow -> salePredictionOffsettingFlow.getSpBizNo() + "_" + salePredictionOffsettingFlow.getSpBizLineNo()).collect(Collectors.toList());
        }
        return paging;
    }
}
